/*
 * ReimsCopieAdminBean.java
 *
 * Created on 5 novembre 2007, 14:57
 */
package com.reimscopie.beans.session ;

import javax.ejb.* ;
import javax.persistence.* ;

import com.reimscopie.* ;
import com.reimscopie.beans.entities.* ;
import com.reimscopie.beans.interfaces.* ;


/**
 * This stateless is the basic admin bean.
 */
@Stateless
@Local(ReimsCopieAdminLocal.class)
@Remote(ReimsCopieAdminRemote.class)
public class ReimsCopieAdminBean extends ReimsCopieAccessBean {
  
  // persistence
  @PersistenceContext()
  private EntityManager entityManager ;
  
  
  /**
   * Create a new <code>ReimsCopieAdminBean</code> instance.
   */
  public ReimsCopieAdminBean() {
    
  }
  
  
  /**
   * Return this persistence manager used by this bean.
   *
   * @return The persistence manager to use.
   */
  @Override
  protected EntityManager getPersistenceManager() {
    return entityManager ;
  }
  
  
////////////////////////////////////////////////////////////////////////////////
//                                ACCOUNT                                     //
////////////////////////////////////////////////////////////////////////////////
  
  /**
   * Create a new account. The account is stored by the persistence manager.
   *
   * @param account The account to create.
   */
  public void createAccount(Account account) {
    
    // test that the login is unused
    AccountBean existing = (AccountBean)executeSingleQuery(entityManager,
        "get-login",account.getLogin()) ;
    if (existing != null)
      throw new IllegalArgumentException("existing login") ;
    
    // instance
    AccountBean accountBean       = new AccountBean(account) ;
    PersonalInfoBean personalBean = new PersonalInfoBean(account.getPersonalInfo()) ;
    
    // link
    accountBean.setPersonalInfo(personalBean) ;
    
    // persistence
    entityManager.persist(personalBean) ;
    entityManager.persist(accountBean) ;
    
  }
  
  
  /**
   * Return the account with the specified id.
   *
   * @param id The account id.
   *
   * @return The specified account, ou <code>null</code> if the account was
   * not found.
   */
  public Account getAccount(long id) {
    return getValue(entityManager,AccountBean.class,id) ;
  }
  
  
  /**
   * Return the account with the specified login name.
   *
   * @param login The account name.
   *
   * @return The specified account, ou <code>null</code> if the login was
   * not found.
   */
  public Account getAccount(String login) {
    
    AccountBean bean = (AccountBean)executeSingleQuery(entityManager,
        "get-login",login) ;
    
    // no result
    if (bean == null)
      return null ;
    
    return bean.toAccount() ;
  }  
  
  
////////////////////////////////////////////////////////////////////////////////
//                                PRODUCT                                     //
////////////////////////////////////////////////////////////////////////////////
  
  /**
   * Create a new simple product. The product is stored by the persistence
   * manager.
   *
   * @param product The product to create.
   */
  public void createSimpleProduct(SimpleProduct product) {
    
    ProductBean bean = new SimpleProductBean(product) ;
    entityManager.persist(bean) ;
    
  }
  
  /**
   * Create a new special product. The product is stored by the persistence
   * manager.
   *
   * @param product The product to create.
   */
  public void createSpecialProduct(SpecialProduct product) {
    
    ProductBean bean = new SpecialProductBean(product) ;
    entityManager.persist(bean) ;
    
  }
  
  
////////////////////////////////////////////////////////////////////////////////
//                               BONS-PLANS                                   //
////////////////////////////////////////////////////////////////////////////////
  
  /**
   * Create a new bon plan.
   *
   * @param bonPlan The valued bon plan.
   */
  public void createBonPlan(BonPlan bonPlan) {
    
    long id = bonPlan.getProduct().getId() ;
    ProductBean product = getEntity(entityManager,ProductBean.class,id) ;
    
    BonPlanBean bean = new BonPlanBean(bonPlan,product) ;
    
    entityManager.persist(bean) ;
    
  }
  
  
////////////////////////////////////////////////////////////////////////////////
//                                ORDERS                                      //
////////////////////////////////////////////////////////////////////////////////
  
  /**
   * Return all referenced orders.
   *
   * @return All orders.
   */
  public Order[] getOrders() {
    
    Object[] objects = executeMultipleQuery(entityManager,"all-orders") ;
    
    Order[] orders = new Order[objects.length] ;
    for (int i=0; i<objects.length;i++) {
      OrderBean bean = (OrderBean)objects[i] ;
      orders[i] = bean.toOrder() ;
    }
    
    return orders ;
    
  }
  
  
  /**
   * Return all referenced orders with the valid status.
   *
   * @return All valid orders.
   */
  public Order[] getValidOrders() {
    
    Object[] objects = executeMultipleQuery(entityManager,"all-valid-orders") ;
    
    Order[] orders = new Order[objects.length] ;
    for (int i=0; i<objects.length;i++) {
      OrderBean bean = (OrderBean)objects[i] ;
      orders[i] = bean.toOrder() ;
    }
    
    return orders ;
    
  }
  
  
  /**
   * Unvalid an order.
   *
   * @param id The order identifier.
   */
  public void unvalidOrder(long id) {
    
    OrderBean order = getEntity(entityManager,OrderBean.class,id) ;
    if (order == null)
      throw new IllegalArgumentException("no such order") ;
    
    order.setValid(false) ;
    entityManager.merge(order) ;
  }
  

////////////////////////////////////////////////////////////////////////////////
//                                COMMENT                                     //
////////////////////////////////////////////////////////////////////////////////
  

  
}
